// pages/tuika/tuika.js
const $api = require('./../../utils/request').API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'jinri',
    timeType: '',
    showTimePicker:false,
    showTime: false,
    startDate:'',
    overData:'',
    dataList: [],
    datas:{},
    info: {},
    minHour: 10,
    maxHour: 20,
    minDate: new Date(2019, 10, 1).getTime(),
    maxDate: new Date(2030, 10, 1).getTime(),
    currentDate: new Date().getTime(),
  },

  haldClick(e){
    this.setData({
      type:e.currentTarget.dataset.type
    })
    if( e.currentTarget.dataset.type == 'jinri' || e.currentTarget.dataset.type == 'zuori' ){
      this.setData({
        showTime: false,
        startDate:'',
        overData:''
      });

      if( e.currentTarget.dataset.type == 'jinri'){
        var day2 = new Date();
        day2.setTime(day2.getTime());
        var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate()+' 23:59:59';
        this.getData(s2);
      }else{
         var day1 = new Date();
         day1.setTime(day1.getTime()-24*60*60*1000);
         var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate()+' 23:59:59';
         this.getData(s1);
      }

    }else{
      this.setData({
        showTime: true,
        info: {}
      })
    }
  },

  timestampToTime(timestamp) {

    var date = new Date(timestamp);

    var Y = date.getFullYear() + '-';

    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';

    var D = date.getDate() + ' ';

    var h = date.getHours() + ':';

    var m = date.getMinutes()<10?  '0'+date.getMinutes()+ ':':date.getMinutes() + ':';

    var s = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds() ;

    return Y+M+D+h+m+s;

},

  showpickerClick(event) {
    if( event.target.dataset.type == 'overData' &&  this.data.startDate == '' ){
      return wx.showToast({
        title: '请选择开始时间',
        icon:'none'
      })
    }
    this.setData({
      timeType: event.target.dataset.type,
      showTimePicker: true
    })
  },

  onconfirm() {
    if (this.data.timeType == 'startDate') {
      this.setData({
        startDate: this.timestampToTime(this.data.currentDate) 
      })
    } else {
      this.setData({
        overData: this.timestampToTime(this.data.currentDate) 
      });
     
    }
    this.setData({
      showTimePicker: false
    });
    this.getData(this.data.startDate, this.data.overData);
  },

  bindStartDateChange: function(e) {
    console.log(e)
    this.setData({
      startDate: e.detail.value
    })
  },
  bindOverDateChange: function(e) {
    if( this.data.startDate == '' ){
      return wx.showToast({
        title: '请选择开始时间',
        icon:'none'
      })
    }
    this.setData({
      overData: e.detail.value
    })


    this.getData(this.data.startDate,this.data.overData);
  },

  getData(time,time2) {
    let params = {};
    this.setData({
      dataList:[],
      datas:{}
    })
    wx.showLoading({
      title: '正在加载',
    })
    if( time2 ){
       params = {
        startTime:time+' 23:59:59',
        endTime: time2+' 23:59:59',
        sailUser:wx.getStorageSync('userId'),
        flag:'3'
      }
    }else{
       params = {
        time: time,
        sailUser:wx.getStorageSync('userId')
      }
    }

    $api.getTuiKa(params).then( res => {
      wx.hideLoading({
        fail:()=>{},
        success:()=>{},
        complete: (complete) => {}
      })
      this.setData({
        dataList: res.data.details,
        datas:res.data
      })
    }).catch(err=>{
      wx.hideLoading({
        fail:()=>{},
        success:()=>{},
        complete: (complete) => {}
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var day2 = new Date();
    day2.setTime(day2.getTime());
    var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate()+' 23:59:59';
    this.getData(s2)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})